package com.pocket.demo.core.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.pocket.demo.business.po.User;

/**
 * @author cookie.
 **/
public class UserContext {
    private static final TransmittableThreadLocal<User> USER_INHERITABLE_THREAD_LOCAL = new TransmittableThreadLocal();

    public static User getUser() {
        return USER_INHERITABLE_THREAD_LOCAL.get();
    }

    public static void setUser(User user) {
        USER_INHERITABLE_THREAD_LOCAL.set(user);
    }

    public static void remove() {
        USER_INHERITABLE_THREAD_LOCAL.remove();
    }

    public static Long getUserId() {
        User user = getUser();
        return user != null ? user.getUserId() : null;
    }
}
